<template>
  <div>
    <el-dialog
      :title="obj.isAdd == true ? '活动编辑' : '活动添加'"
      :visible.sync="obj.isTrue"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="活动期限">
          <el-date-picker
            v-model="value1"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="一级分类">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择一级分类"
            @change="changeone"
          >
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="(item, index) in cateList"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select
            v-model="form.second_cateid"
            placeholder="请选择二级分类"
            @change="changetow"
          >
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="item in secondarr"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择商品">
          <el-select v-model="form.goodsid" placeholder="请选择商品">
            <el-option
              :label="item.goodsname"
              :value="item.id"
              v-for="item in secondarr2"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submit()" v-if="obj.isAdd == false"
          >添加</el-button
        >
        <el-button type="primary" v-else @click="dit()">编辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  cateList,
  seckDel,
  seckAdd,
  seckInfo,
  seckEdit,
  goodsList,
} from "../../../requster/api";
import { successAlert, warningAlert } from "../../../util/alter";
import E from "wangeditor";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: "",
      },
      value1: [],

      // 渲染二级分类
      secondarr: [],
      // 渲染3级分类
      secondarr2: [],
      // 渲染规格属性
    };
  },
  computed: {
    ...mapGetters({
      cateList: "cate/list",
      specsList: "specs/list",
      goodsList: "goods/list",
    }),
  },
  methods: {
    //   动画结束之后

    //   获取一条信息
    getone(id) {
      this.value1.push(Number(this.form.begintime));
      this.value1.push(Number(this.form.endtime));

      seckInfo({ id: id }).then((res) => {
        this.form = res.data.list;
        // 根据拿到的一级分类渲染二级分类
        cateList({ pid: this.form.first_cateid }).then((res) => {
          this.secondarr = res.data.list;
          this.form.id = id;
        });

        // 根据拿到的二级分类渲染
        goodsList({
          fid: this.form.first_cateid,
          pid: this.form.second_cateid,
        }).then((res) => {
          this.secondarr2 = res.data.list.filter(
            (item) => item.second_cateid == this.form.second_cateid
          );
          // 清空3级
          this.form.goodsid = "";
        });
      });
    },
    //   点击编辑
    dit(id) {
      console.log(this.form);
      seckEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqsecklist();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    //   清空封装
    empt() {
      this.imageUrl = "";
      this.secondarr = [];
      // 渲染规格属性
      this.specsattr = [];
      this.form = {
        id: "",
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: "",
      };
    },
    // 点击取消
    cancel() {
      this.obj.isTrue = false;
      this.empt();
    },
    // 点击添加
    submit() {
      this.form.begintime = new Date(this.value1[0]).getTime();
      this.form.endtime = new Date(this.value1[1]).getTime();

      seckAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          this.cancel();
          this.reqsecklist();
          successAlert(res.data.msg);
        } else {
          warningAlert(res.data.msg);
        }
      });
      res.data.list.id = this.form.id;
    },
    ...mapActions({
      reqgoodslist: "goods/reqchangelist",
      reqsecklist: "seckill/reqChangeList",
      reqcatelist: "cate/reqChangeList",
      reqspecslist: "specs/reqchangelist",
    }),
    // 当一级分类改变
    changeone() {
      cateList({ pid: this.form.first_cateid }).then((res) => {
        this.secondarr = res.data.list;
        // 清空二级分类
        this.form.second_cateid = "";

        console.log(1);
      });
    },
    // 当二级分类改变
    changetow() {
      this.secondarr2 = [];
      goodsList({
        fid: this.form.first_cateid,
        pid: this.form.second_cateid,
      }).then((res) => {
        this.secondarr2 = res.data.list.filter(
          (item) => item.second_cateid == this.form.second_cateid
        );
        // 清空3级
        this.form.goodsid = "";
      });
    },
  },
  mounted() {
    this.reqcatelist();
    this.reqspecslist(true);
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>